﻿@{
    ViewBag.Title = "Score";
}



<script src="~/js/knockout-3.4.1.js"></script>

<h2>成绩管理</h2>




<div class="container-fluid">
    <div class="row">

        <nav class="col-md-2 col-xs-12">

            <ul class="nav nav-pills nav-stacked">
                <li><a href="/Home/Index">学生管理</a></li>
                <li><a href="/Home/Class">班级管理</a></li>
                <li ><a href="/Home/Course">课程管理</a></li>
                <li class ="active"><a href="/Home/Score">成绩管理</a></li>
                <li><a href="#">退出</a></li>

            </ul>
        </nav>
        <div id="content" class="col-md-10 col-xs-12">

            <button type="button" class="btn" data-toggle="modal" data-target="#panelUser" data-bind="click:$root.AddNewBefore">加入新成绩</button>
            <table class="table">
                <thead>
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            学生
                        </th>
                        <th>
                           课程
                        </th>
                        <th>
                            成绩
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody data-bind="foreach: Scores">
                    <tr>
                        <td data-bind="text:Id"></td>
                        <td data-bind="text:SName"></td>
                        <td data-bind="text:SCName"></td>
                        <td data-bind="text:FScore"></td>
                        <td>

                            <a href="javascript:void(0);" data-bind="click:$root.EditScore">编辑</a>


                            <a href="javascript:void(0);" data-bind="click:$root.DelScore">删除</a>

                        </td>
                    </tr>

                </tbody>

            </table>


        </div>




    </div>
</div>




<div class="modal fade" tabindex="-1" role="dialog" id="panelUser">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">成绩详情</h4>
            </div>
            <div class="modal-body">

                <form data-bind="with:ScoreItem">
                    <div class="form-group">
                        <label for="exampleInputEmail1">学生:</label>

                        @Html.DropDownList("txtbSName", new SelectList(ViewBag.allStudent, "Id", "SName"), new { @data_bind = "value:IStudentN", @class = "form-control"})


                    </div>
                    <div class="form-group">
                        <label for="exampleInputPassword1">课程名：</label>
                        @Html.DropDownList("txtbSCName", new SelectList(ViewBag.allCourse, "Id", "SCName"), new { @data_bind = "value:ICoureN", @class = "form-control", })
                    </div>

                    <div class="form-group">
                        <label for="exampleInputPassword1">成绩：</label>
                        <input type="text" class="form-control" id="txtbFScore" placeholder="成绩" data-bind="value:FScore">
                    </div>
                </form>



            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-bind="click:AddNewScore">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script type="text/javascript">



    function ViewModel() {
        var self = this;

        self.ScoreItem = ko.observable({
            Id: 0, IStudentN: 0, ICoureN: 0, FScore: 0
        });//一个关于用户的可观察对象


        self.Scores = ko.observableArray([]);


        self.DelScore = function (a, b, c, d) {

            if (confirm("真的要删除此项吗？")) {
                $.ajax({
                    url: '/Home/DelScore',
                    data: { ID: a.Id },
                    type: 'post',
                    success: function (result) {

                        if (result != null && result.ID != null && result.ID > 0) {
                            myView.getScore();//重新刷新前面的数据

                        } else if (result.ID <= 0) {

                            alert(result.Msg);//一般出错
                        } else {
                            alert("出错了!" + result);//一般出错
                        }





                    }//end success
                });

            }



        }


        self.EditScore = function (a, b, c, d) {
            self.ScoreItem(a);

            $("#panelUser").modal('show');
        }

        self.AddNewBefore = function (a, b, c, d) {
            self.ScoreItem({
                Id: 0, IStudentN: 0, ICoureN: 0, FScore: 0
            });//一个关于用户的可观察对象
        }

        self.AddNewScore=function() {
            var txtbIStudentN = myView.ScoreItem().IStudentN;
            var txtbICoureN = myView.ScoreItem().ICoureN;
            var scoreV = myView.ScoreItem().FScore;
            if (txtbIStudentN <= 0 || txtbICoureN<=0) {
                alert("出错了,课程和学生都不能为空!!!");
                return false;
            }




            $.ajax({
                url: '/Home/AddOrUpdateScore',
                data: {
                    Id: myView.ScoreItem().Id,
                    StuID: myView.ScoreItem().IStudentN,
                    CourseID: myView.ScoreItem().ICoureN,
                    score: scoreV
                },
                type:'post',
                success: function (result) {

                    myView.getScore();//重新刷新前面的数据

                    $("#panelUser").modal('hide');


                }//end success
            });
            //end ajax

        }//end AddNewCourse



        self.getScore=function() {
            $.ajax({
                url: '/Home/getScore',

                type: 'post',
                success: function (result) {

                    myView.Scores(result);




                }//end success
            });
            //end ajax

        };


    }

    var myView = new ViewModel();

    $(function () {


        myView.getScore();
        ko.applyBindings(myView);


    });
</script>